package com.mxpio.erp.inventory.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.inventory.entity.SupplyChain;
import com.mxpio.erp.common.inventory.service.SupplyChainService;
import com.mxpio.erp.common.purchase.entity.Supply;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.vo.SupplyChainVo;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "SupplyChainController", description = "供应链接口")
@RestController
@RequestMapping("/erp/inventory/supplychain/")
public class SupplyChainController {
	
	@Autowired
	private SupplyChainService supplyChainSerivce;
	
	@GetMapping("supply/page")
	@Operation(summary = "供应链列表(按供应商)", description = "获取供应链列表(按供应商)", method = "GET")
	public Result<Page<Supply>> pageBySupply(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<Supply> page = supplyChainSerivce.pageBySupply(pageAble, criteria);
		return Result.OK(page);
	}
	
	@GetMapping("item/page")
	@Operation(summary = "供应链列表(按物料)", description = "获取供应链列表(按物料)", method = "GET")
	public Result<Page<Item>> pageByItem(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<Item> page = supplyChainSerivce.pageByItem(pageAble, criteria);
		return Result.OK(page);
	}

	@GetMapping("supplyno/itempage/{pnCode}")
	@Operation(summary = "具体供应商的物料关系明细-分页", description = "具体供应商的物料关系明细-分页", method = "GET")
	public Result<Page<Item>> pnCodeOwnedItemsPage(Criteria criteria, Integer pageSize, Integer pageNo, @PathVariable(name = "pnCode", required = true) String pnCode) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<Item> page = supplyChainSerivce.pnCodeOwnedItemsPage(pageAble, criteria,pnCode);
		return Result.OK(page);
	}

	@GetMapping("supply/list/{itemCodes}")
	@Operation(summary = "由物料code通过供应链关系查询对应共有供应商列表", description = "由物料code通过供应链关系查询对应共有供应商列表", method = "GET")
	public Result<List<Supply>> getSupplysByItemCodes(@Parameter(description="物料代码，多个单号用英文逗号分隔") @PathVariable(name = "itemCodes", required = true) String itemCodes) {
		String[] itemCode = itemCodes.split(",");
		List<Supply> list = supplyChainSerivce.getSupplysByItemCodes(itemCode);
		return Result.OK(list);
	}
	@GetMapping("supply/machinedlist/{itemCodes}")
	@Operation(summary = "由物料code通过供应链关系查询对应共有供应商（加工商）列表", description = "由物料code通过供应链关系查询对应共有供应商（加工商）列表", method = "GET")
	public Result<List<Supply>> getMachinedSupplysByItemCodes(@Parameter(description="物料代码，多个单号用英文逗号分隔") @PathVariable(name = "itemCodes", required = true) String itemCodes) {
		String[] itemCode = itemCodes.split(",");
		List<Supply> list = supplyChainSerivce.getMachinedSupplysByItemCodes(itemCode);
		return Result.OK(list);
	}

	@RequestMapping("save")
    @Operation(summary = "保存供应链", description = "保存供应链", method = "POST")
    public Result<List<SupplyChain>> save(@RequestBody List<SupplyChain> supplyChains) {
		supplyChainSerivce.save(supplyChains);
        return Result.OK(supplyChains);
    }
	
	@DeleteMapping("supply/remove/{pnCode}/{itemCodes}")
    @Operation(summary = "根据itemCodes删除供应链", description = "根据itemCode删除供应链", method = "DELETE")
    public Result<SupplyChain> removeBySupply(@PathVariable(name = "pnCode", required = true) String pnCode
    		,@Parameter(description="物料代码，多个单号用英文逗号分隔") @PathVariable(name = "itemCodes", required = true) String itemCodes) {
        String[] itemCode = itemCodes.split(",");
        supplyChainSerivce.removeBySupply(pnCode, itemCode);
        return Result.OK();
    }
	
	@DeleteMapping("item/remove/{itemCode}/{pnCodes}")
    @Operation(summary = "根据pnCodes删除供应链", description = "根据pnCodes删除供应链", method = "DELETE")
    public Result<SupplyChain> removeByItem(@PathVariable(name = "itemCode", required = true) String itemCode
    		,@Parameter(description="供应商代码，多个单号用英文逗号分隔") @PathVariable(name = "pnCodes", required = true) String pnCodes) {
        String[] pnCode = pnCodes.split(",");
        supplyChainSerivce.removeByItem(itemCode, pnCode);
        return Result.OK();
    }
	
	@PutMapping("supply/edit/{pnCode}/{itemCodes}")
    @Operation(summary = "根据pnCode批量修改供应链", description = "根据pnCode批量修改供应链", method = "PUT")
	public Result<Object> editBatchBySupply(@PathVariable(name = "pnCode", required = true) String pnCode
			,@Parameter(description="物料代码，多个单号用英文逗号分隔") @PathVariable(name = "itemCodes", required = true) String itemCodes
			,@RequestBody SupplyChainVo supplyChainVo){
		String[] itemCode = itemCodes.split(",");
		supplyChainSerivce.editBatchBySupply(pnCode, itemCode, supplyChainVo);
		return Result.OK();
	}
	
	@PutMapping("item/edit/{itemCode}/{pnCodes}")
    @Operation(summary = "根据itemCode批量修改供应链", description = "根据itemCode批量修改供应链", method = "PUT")
	public Result<Object> editBatchByItem(@PathVariable(name = "itemCode", required = true) String itemCode
			,@Parameter(description="供应商代码，多个单号用英文逗号分隔") @PathVariable(name = "pnCodes", required = true) String pnCodes
			,@RequestBody SupplyChainVo supplyChainVo){
		String[] pnCode = pnCodes.split(",");
		supplyChainSerivce.editBatchByItem(itemCode, pnCode, supplyChainVo);
		return Result.OK();
	}
	
	@GetMapping("without/items/page/{pnCode}")
	@Operation(summary = "未匹配物料列表(按供应商)", description = "获取未匹配物料列表(按供应商)", method = "GET")
	public Result<Page<Item>> withoutItemBySupply(@PathVariable(name = "pnCode", required = true) String pnCode,
			Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<Item> page = supplyChainSerivce.withoutItemBySupply(pnCode, pageAble, criteria);
		return Result.OK(page);
	}
	
	@GetMapping("without/supplies/page/{itemCode}")
	@Operation(summary = "未匹配的供应商列表(按物料)", description = "获取未匹配的供应商列表(按物料)", method = "GET")
	public Result<Page<Supply>> withoutSupplyByItem(@PathVariable(name = "itemCode", required = true) String itemCode,
			Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<Supply> page = supplyChainSerivce.withoutSupplyByItem(itemCode, pageAble, criteria);
		return Result.OK(page);
	}

	@GetMapping("get_item_price/{itemCode}/{pnCode}")
	@Operation(summary = "获取物料价格", description = "获取物料价格(先从供应链关系去取，取不到再取物料基础信息中的价格)", method = "GET")
	@Transactional(readOnly = true)
	public Result<Item> getItemPrice(@Parameter(description = "itemCode物料编码") @PathVariable(name = "itemCode", required = true) String itemCode,
														   @Parameter(description = "pnCode，加工商编码") @PathVariable(name = "pnCode", required = true) String pnCode) throws UnsupportedEncodingException {
		return supplyChainSerivce.getItemPrice(itemCode,pnCode);
	}
}
